/**
 * 
 */
package com.ys.ylideas.util;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.ylideas.entity.IdeaEntity;

/**
 * @author Red
 * 用于http请求的包装类
 */
public class CreativeJsonRequest {
	private JSONObject header;
	private JSONObject params;
	
	private JSONObject body;
	private JSONObject requestData;
	private JSONObject totalJson;
	
	public void init(){
		header = new JSONObject();
		params = new JSONObject();
		
		body = new JSONObject();
		requestData = new JSONObject();
		totalJson = new JSONObject();
				
	}
	
	public JSONObject getJsonData(String accessToken,JSONObject jsonParams){
		init();
				
		params.put("token", CommonData.token);
		params.put("accessToken", accessToken);		
		header.put("header", params);
		
		
		body.put("body", jsonParams);
		totalJson.putAll(header);
		totalJson.putAll(body);
		
		
		
		//System.out.println("totalJson:"+JSONObject.toJSONString(totalJson, SerializerFeature.WriteMapNullValue));
		return totalJson;		
	}
	
	// 上传或者更新创意（单个）
	public String addOrUpdate(String access_token,IdeaEntity ideas,int type){
		init();
		
		// type包装类型  1:update 0:add
		// 处理头部
		params.put("token", CommonData.token);
		params.put("accessToken", access_token);
		header.put("header", params);
		
		JSONArray ideaArray = new JSONArray();
		JSONObject ideaObject = new JSONObject();
		
		if(type == 1){
			ideaObject.put("creativeId", ideas.getCreativeId());
		}else if(type == 0){
			ideaObject.put("adgroupId", ideas.getAdgroupId());
		}		
		ideaObject.put("title", ideas.getIdeaTitle());
		ideaObject.put("description1", ideas.getIdeaDesOne());
		ideaObject.put("description2", ideas.getIdeaDesTwo());
		ideaObject.put("pcDestinationUrl", ideas.getVisitUrl());
		ideaObject.put("pcDisplayUrl", ideas.getDisplayUrl());
		ideaObject.put("mobileDestinationUrl", ideas.getMobileVisitUrl());
		ideaObject.put("mobileDisplayUrl", ideas.getMobileDisplayUrl());
		ideaArray.add(ideaObject);
		// 处理主体
		requestData.put("creativeTypes", ideaArray);
		body.put("body", requestData);
		
		
		// 拼接
		totalJson.putAll(header);
		totalJson.putAll(body);
		
		return totalJson.toString();
	}
	
	// 批量上传创意
	public String addList(String access_token,List<IdeaEntity> ideas){
		init();

		// 处理头部
		params.put("token", CommonData.token);
		params.put("accessToken", access_token);
		header.put("header", params);
		
		JSONArray ideaArray = new JSONArray();
		for(int i = 0;i<ideas.size();i++){
			JSONObject ideaObject = new JSONObject();
			ideaObject.put("adgroupId", ideas.get(i).getAdgroupId());
			ideaObject.put("title", ideas.get(i).getIdeaTitle());
			ideaObject.put("description1", ideas.get(i).getIdeaDesOne());
			ideaObject.put("description2", ideas.get(i).getIdeaDesTwo());
			ideaObject.put("pcDestinationUrl", ideas.get(i).getVisitUrl());
			ideaObject.put("pcDisplayUrl", ideas.get(i).getDisplayUrl());
			ideaObject.put("mobileDestinationUrl", ideas.get(i).getMobileVisitUrl());
			ideaObject.put("mobileDisplayUrl", ideas.get(i).getMobileDisplayUrl());
			ideaArray.add(ideaObject);
		}
		
		// 处理主体
		requestData.put("creativeTypes", ideaArray);
		body.put("body", requestData);
				
		// 拼接
		totalJson.putAll(header);
		totalJson.putAll(body);
		
		return totalJson.toString();
				
	}
	
	// 获取用户账户创意
	public String get(String access_token,Long[] ids){
		init();
		// 处理头部
		params.put("token", CommonData.token);
		params.put("accessToken", access_token);
		header.put("header", params);
		
		String[] creativeFields = {"adgroupId",
				"creativeId",
				"title",
				"description1",
				"description2",
				"pcDestinationUrl",
				"mobileDestinationUrl",
				"pcDisplayUrl",
				"mobileDisplayUrl",
				"pause",
				"status",
				"devicePreference"};
		
		// 处理主体
		requestData.put("creativeFields", creativeFields);
		requestData.put("idType", 7);
		requestData.put("ids", ids);
		body.put("body", requestData);
		
		// 拼接
		totalJson.putAll(header);
		totalJson.putAll(body);
		
		return totalJson.toString();
	}
	
	
	public String delete(String access_token,Long[] ids){
		init();
		
		params.put("token", CommonData.token);
		params.put("accessToken", access_token);
		header.put("header", params);
		
		
		requestData.put("creativeIds", ids);
		body.put("body", requestData);
		
		totalJson.putAll(header);
		totalJson.putAll(body);
		
		return totalJson.toString();
	}
}
